<?php
session_start();
      if (isset($_SESSION['k_username'])) {
include ("conexion.inc.php");
$link = Conectarse();
$editadopor=$_SESSION['k_username'];
$fecha = date("y-m-d h:i:s");
mysql_select_db("gotica_ordencompras" ,$link);
?>
<html><head><title>Orden Registrado con Exito</title>

</head>
<body>
<?php
$upd="UPDATE ordencompra SET Observaciones=$_POST[observaciones] Where idorden=$_POST[idorden]";
$resultu = mysql_query($upd);

$sql="SELECT * FROM ordencompra Where idorden=$_POST[idorden]";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);
$total=0;
$sqlauth="SELECT * FROM Gestionador WHERE idgestionador=".$row[idgestionador];
$resultauth= mysql_query($sqlauth);
$auth =mysql_fetch_array($resultauth);
?>
<table width="100%" height="100%" border="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><table width="100%" border="0">
      <tr>
        <td width="9%">Cliente</td>
<?
$sql2="SELECT * FROM proveedor WHERE idproveedor=".$row[idproveedor];
$result2 = mysql_query($sql2);
$row2=mysql_fetch_array($result2);

echo "<td width=\"55%\">$row2[N_proveedor]</td>"; ?>
        <td width="9%">N Orden</td>
 <? echo "<td width=\"27%\">$row[idorden]</td>"; ?>
      </tr>
      <tr>
        <td>Direccion:</td>
 <?      
echo "<td>$row2[T_direccion]</td>"; ?>
        <td>Contrato</td>
       <td>&nbsp;</td>
      </tr>
      <tr>
        <td>RUC</td>
<? echo "<td>$row2[T_RUC]</td>"; ?>
        <td>Contacto</td>
<? echo "<td>$_GET[editado]</td>"; ?>
      </tr>
      <tr>
        <td>Medio</td>
<?   
$sql3="SELECT * FROM cliente WHERE idcliente=".$row[idcliente];
$result3 = mysql_query($sql3);
$row3=mysql_fetch_array($result3);

echo "   <td>$row3[N_Cliente]</td>"; ?>

        <td>Ref</td>
<?   
$sql30="SELECT * FROM Area WHERE idarea=".$row[idarea];
$result30 = mysql_query($sql30);
$row30=mysql_fetch_array($result30);

echo "   <td>$row30[area]</td>"; ?>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td>Detalle Pedido</td>
	<td><table align="left" border="0">
	
	<? 
	$sql4="SELECT * FROM detallepedido WHERE idorden=".$row['idorden'];
$result4 = mysql_query($sql4);
while ($row4=mysql_fetch_array($result4)){
	echo "<tr><td>";
	$cuerpo1.="<tr><td>";
	$sql5="SELECT producto.N_producto , unidad.n_unidad  FROM producto, unidad WHERE producto.idproducto=".$row4['idproducto']." and idunidad=".$row4['idunidad'];
$result5 = mysql_query($sql5);
$row5=mysql_fetch_array($result5);
    echo $row5['N_producto']; 
	$cuerpo1.="$row5[N_producto]";
$sql6="SELECT preciounitario  FROM prod_provdo WHERE idproducto=".$row4['idproducto']." and idproveedor=".$row[idproveedor];
$result6 = mysql_query($sql6);
$row6=mysql_fetch_array($result6);
$total+=$row4['Q_cantidad']*$row6['preciounitario'];
	echo "</td>";
	$cuerpo1.="</td>";
	echo "<td>".$row4['Q_cantidad']."</td>";
	$cuerpo1.="<td>$row4[Q_cantidad]</td>";
	echo "<td>".$row5['n_unidad']."</td>";
	$cuerpo1.="<td>$row5[n_unidad]</td>";
	echo "</tr>";
	$cuerpo1.="</tr>";	}

	?>
	
	</table></td>
  </tr>
  <tr>
    <td>Producto</td>
  </tr>
  <tr>
    <td><p>Facturar a:</p>
      <table width="100%" border="0">
        <tr>
          <td width="12%">Razon Social:</td>
  <?  echo "<td width=\"52%\">$row3[N_Cliente]</td> "; ?>
          <td width="12%">Total Neto</td>
  <?
 
echo "<td width=\"24%\">S/. $total</td> ;";

?>
        </tr>
        <tr>
          <td>Direccion:</td>
<? echo "<td>$row3[T_direccion]</td>"; ?>
          <td>IGV</td>
	<?  $igv=$total*0.19;
	echo " <td>S/. $igv</td>"; ?>
        </tr>
        <tr>
          <td>RUC:</td>
       <? echo "<td>$row3[T_RUC]</td>"; ?>
          <td>Total</td>
		<?
		$tot=$total+$igv;
		echo "<td> S/. $tot</td>"; ?>
        </tr>
        <tr>
          <td>Observaciones:</td>
    <? echo "<td><textarea disabled rows=\"10\" cols=\"30\" >$row[Observaciones]</textarea></td>"; ?>

        </tr>
      </table>      <p>&nbsp;</p></td>
  </tr>
<tr>
<td>
<? echo " <img src=\"".$auth['firma']."\" />"; ?>
</td>
</tr>
  <tr>
    <td><p>____________________________ </p>
      <p>Firma Cliente</p></td>
  </tr>
</table>

</body>
</html>

<?
$cuerpo="<html>
<head>
   <title>Nueva Orden</title>
</head>
<table width=\"100%\" height=\"100%\" border=\"0\">
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><table width=\"100%\" border=\"0\">
      <tr>
        <td width=\"9%\">Cliente</td>

<td width=\"55%\">$row2[N_proveedor]</td>
        <td width=\"9%\">N Orden</td>
 <td width=\"27%\">$row[idorden]</td>
      </tr>
      <tr>
        <td>Direccion:</td>
<td>$row2[T_direccion]</td>
        <td>Contrato</td>
       <td>&nbsp;</td>
      </tr>
      <tr>
<td>RUC</td>
<td>$row2[T_RUC]</td>
        <td>Contacto</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Medio</td>

  <td>$row3[N_Cliente]</td>
        <td>Ref</td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width=\"10%\">Detalle Pedido</td>
	<td><table align=\"left\" border=\"0\">";
$cuerpo.="$cuerpo1";
	
	$cuerpo.="</table></td>
  </tr>
  <tr>
    <td>Producto</td>
  </tr>
  <tr>
    <td><p>Facturar a:</p>
      <table width=\"100%\" border=\"0\">
        <tr>
          <td width=\"12%\">Razon Social:</td>
<td width=\"52%\">$row3[N_Cliente]</td> 
          <td width=\"12%\">Total Neto</td>
   
<td width=\"24%\"> S/. $total</td> 
        </tr>
        <tr>
          <td>Direccion:</td>
<td>$row3[T_direccion]</td>
          <td>IGV</td>
		<td> S/. $igv</td>
        </tr>
        <tr>
          <td>RUC:</td>
<td>$row3[T_RUC]</td>
          <td>Total</td>
<td> S/. $tot</td>
        </tr>
        <tr>
          <td>Observaciones:</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>      <p>&nbsp;</p></td>
  </tr>
<tr>
<td>
<img src=\"$auth[firma]\" />
</td>
</tr>
  <tr>
    <td><p>____________________________ </p>
      <p>Firma Cliente</p></td>
  </tr>

</table>";
mysql_select_db("gotica_sociosgotica" ,$link);
$sql10="SELECT * FROM usuarios WHERE usuario='$_SESSION[k_username]'";
$result10=mysql_query($sql10,$link);
$row10=mysql_fetch_array($result10);
$remitente = $row10['email'];
$nomremitente= $row10['descripcion'];
//para el envío en formato HTML
$headers = "Content-type: text/html\r\n";

//dirección del remitente
$headers .= "From: $nomremitente <$remitente>\r\n";

//dirección de respuesta, si queremos que sea distinta que la del remitente
$headers .= "Reply-To: $remitente\r\n";
$destinatario=$auth['email'];
$asunto = "ORDEN ACEPTADA";
mail($destinatario,$asunto,$cuerpo,$headers);
?>
<?
}else
{
	echo "<SCRIPT LANGUAGE=\"javascript\">";
echo "location.href = \"index.php\"";
echo "</SCRIPT>";
	}
?>